﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Elementum
{
    public class GameKey : Item
    {
        public GameKey(World world, Stage stage)
            : base(world, stage)
        {
            _IsCarring = false;
            CurrentAnimation = new Animation("images/items/key", new Point(23, 35));
        }

        public GameKey(String Id, World world, Stage stage)
            : base(Id, world, stage)
        {
            _IsCarring = false;
            CurrentAnimation = new Animation("images/items/key", new Point(23, 35));
            CurrentAnimation.LayerDepth = 0.1f;
            if (!StageManager.Save.StageStateTable[StageManager.CurrentStateID].ItemStateTable.ContainsKey(ItemID))
            {
                ItemState itemState = new ItemState();

                try
                {
                    itemState.StageID = StageManager.CurrentStateID;
                    StageManager.Save.StageStateTable[StageManager.CurrentStateID].ItemStateTable.Add(ItemID, itemState);
                }
                catch (ArgumentException)
                {
                    throw new Exception("An GameKey with Key = " + ItemID + " already exists.");
                }

            }

            _isUsed = StageManager.Save.StageStateTable[StageManager.CurrentStateID].ItemStateTable[ItemID]._isUsed;
            _IsCarring = StageManager.Save.StageStateTable[StageManager.CurrentStateID].ItemStateTable[ItemID]._IsCarring;
            if (IsCarring)
            {
                _filter.maskBits = 0x00;
                _owner = GameStage.player;
                _owner.BeginContact += OpenDoor;
            }

            
        }


        public override void CreatBody(World world)
        {
            this._world = world;
            BodyDef bodyDef = new BodyDef();
            _body = world.CreateBody(bodyDef);
            _body.Owner = this;
            _body._userData = "Key";
            _body._type = BodyType.Dynamic;
            KeyID = "Door";
        }

        public string KeyID { get; set; }

        public override void Update(int delta)
        {
            base.Update(delta);

            if (_isUsed)
                Position = new Vector2(Position.X, Position.Y + 2000);


            if (_IsCarring)
            {
                if (_owner.GetBody().GetUserData() == "player")
                {
                    Player player = (Player)_owner;
                    if (player._playerState == PlayerState.Left)
                    {
                        Position = _owner.Position + new Vector2(-15, 0);
                    }
                    else if (player._playerState == PlayerState.Right)
                    {
                        Position = _owner.Position + new Vector2(15, 0);
                    }
                }
                else
                {
                    Monster monster = (Monster)_owner;
                    //Position = _owner.Position + new Vector2(0, -20);
                    if (monster.Speed.X > 0f)
                    {
                        Position = _owner.Position + new Vector2(0, -10);
                    }
                    else if (monster.Speed.X < 0f)
                    {
                        Position = _owner.Position + new Vector2(0, -10);
                    }
                }

            }
        }

        public override void OnDraw(SpriteBatch spriteBatch, Matrix transform)
        {
            if(!_isUsed)
            base.OnDraw(spriteBatch, transform);
        }

        public override void ThrowOutItem()
        {
            _IsCarring = false;
            Position = _owner.Position + new Vector2(0, -50);
            _owner.BeginContact -= OpenDoor;
            Monster monster = (Monster)_owner;

            if (monster.Speed.X > 0)
            {
                ConfigFilter();
                //_filter.maskBits -= PlayerCategory;
                _fixture.SetFilterData(ref _filter);
                _body.SetLinearVelocity(new Vector2(_speed, 2.5f));
            }
            if (monster.Speed.X < 0)
            {
                ConfigFilter();
                // _filter.maskBits -= PlayerCategory;
                _fixture.SetFilterData(ref _filter);
                _body.SetLinearVelocity(new Vector2(-_speed, 2.5f));
            }

        }

        public override void BeginContactAction(Contact contact, Body contactBody, float normal)
        {
            base.BeginContactAction(contact, contactBody, normal);
            if (!IsUsed && contactBody.GetUserData() == "player")
            {
                _owner.BeginContact += OpenDoor;
            }

            if (!IsUsed && contactBody.GetUserData() == "monster")
            {
                Monster monster = (Monster)_owner;
                if (monster._item == null)
                {
                    _owner.BeginContact += OpenDoor;
                    monster._item = this;
                }
            }

        }

        public void OpenDoor(Contact contact, Body contactBody, float normal)
        {
            if (contactBody.GetUserData()=="door")              
            {
                GameDoor door = (GameDoor)contactBody.Owner;
                if (door.DoorID==KeyID&& door.IsOpened == false)
                {
                    door.ChangeState();
                    _owner.BeginContact -= OpenDoor;
                    if (_owner.GetBody().GetUserData() == "monster") {
                        Monster monster = (Monster)_owner;
                        monster._item = null;
                    }

                    IsUsed = true;
                    IsCarring = false;
                    _body.SetType(BodyType.Static);
                    //Position = new Vector2(Position.X, Position.Y + 2000);
                }
            }
        }

        public override void EndContactAction(Contact contact, Body contactBody, float normal)
        {
            base.EndContactAction(contact, contactBody, normal);
        }

    }
}
